package net.baribeau.pdfgen;

import java.io.*;



import org.w3c.dom.Document;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Element;

/**
 * @author Sean Baribeau
 * 
 */
public class TransformData extends HttpServlet {

	private static final long serialVersionUID = 1L;

	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		Object fName = request.getParameter("fName");
		Object address = request.getParameter("address");
		Object email = request.getParameter("email");
		Object phone = request.getParameter("phone");
		Object soq = request.getParameter("soq");
		Object company = request.getParameter("company");
		Object workDate = request.getParameter("workDate");
		Object desc = request.getParameter("desc");
		Object company2 = request.getParameter("company2");
		Object workDate2 = request.getParameter("workDate2");
		Object desc2 = request.getParameter("desc2");
		Object school = request.getParameter("school");
		Object studyDate = request.getParameter("studyDate");
		Object awards = request.getParameter("awards");
		Object school2 = request.getParameter("school2");
		Object studyDate2 = request.getParameter("studyDate2");
		Object awards2 = request.getParameter("awards2");
		Object refName1 = request.getParameter("refName1");
		Object refName2 = request.getParameter("refName2");
		Object refName3 = request.getParameter("refName3");
		Object refPhone1 = request.getParameter("refPhone1");
		Object refPhone2 = request.getParameter("refPhone2");
		Object refPhone3 = request.getParameter("refPhone3");
		
		Document xmlDocument  = null;
		
		
		
			try {
				xmlDocument = XmlBuilderUtil.createDocument();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			}
		
	
		Element resume = xmlDocument.createElement("resume"); 
			
		XmlBuilderUtil.addElement(resume, "fName", fName);
		XmlBuilderUtil.addElement(resume, "address", address);
		XmlBuilderUtil.addElement(resume, "email", email);
		XmlBuilderUtil.addElement(resume, "phone", phone);
		XmlBuilderUtil.addElement(resume, "soq", soq);
		XmlBuilderUtil.addElement(resume, "company", company);
		XmlBuilderUtil.addElement(resume, "workDate", workDate);
		XmlBuilderUtil.addElement(resume, "desc", desc);
		XmlBuilderUtil.addElement(resume, "company2", company2);
		XmlBuilderUtil.addElement(resume, "workDate2", workDate2);
		XmlBuilderUtil.addElement(resume, "desc2", desc2);
		XmlBuilderUtil.addElement(resume, "school", school);
		XmlBuilderUtil.addElement(resume, "studyDate", studyDate);
		XmlBuilderUtil.addElement(resume, "awards", awards);
		XmlBuilderUtil.addElement(resume, "school2", school2);
		XmlBuilderUtil.addElement(resume, "studyDate2", studyDate2);
		XmlBuilderUtil.addElement(resume, "awards2", awards2);
		XmlBuilderUtil.addElement(resume, "refName1", refName1);
		XmlBuilderUtil.addElement(resume, "refName2", refName2);
		XmlBuilderUtil.addElement(resume, "refName3", refName3);
		XmlBuilderUtil.addElement(resume, "refPhone1", refPhone1);
		XmlBuilderUtil.addElement(resume, "refPhone2", refPhone2);
		XmlBuilderUtil.addElement(resume, "refPhone3", refPhone3);

		xmlDocument.appendChild(resume);
		
		final Source xmlSource = XmlBuilderUtil.getSource(xmlDocument); 
		// TODO
		final Source xslSource = new StreamSource(new File("C:/Documents and Settings/isDc765/workspace/pdf-gen-webapp/WebContent/transform.xsl"));
		
		XmlRenderer renderer = new XmlRenderer();
		
		// TODO
		response.setContentType("");
		
		try {
			renderer.createPdf(xmlSource, xslSource, response.getOutputStream());
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}// doPost
} // End of TransformData 